package com.example.register1.service.impl;

import com.example.register1.domin.Detail;
import com.example.register1.mapper.DetailMapper;
import com.example.register1.mapper.RegisterMapper;
import com.example.register1.result.Result;
import com.example.register1.service.DetailService;
import com.example.register1.vo.DetailVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class DetailServiceImpl implements DetailService {

    @Autowired
    private RegisterMapper registerMapper;


    @Autowired
    private DetailMapper detailMapper;


    @Override
    public Result<DetailVO> selectByName(String name) {

        //找到最小时间
        LocalDateTime dateMinTime = registerMapper.selectMinDate();

        //找到最大时间
        LocalDateTime dateMaxTime = registerMapper.selectmaxDate();

        //表中无签到记录
        if(dateMinTime==null){
            return Result.success(null);
        }

        //转化为LocalDate格式
        LocalDate localDateMin = dateMinTime.toLocalDate();
        LocalDate localDateMax = dateMaxTime.toLocalDate();

        //创建结果集合
        List<DetailVO.Student> list = new ArrayList<>();
        
        //创建返回结果对象
        DetailVO detailVO = new DetailVO();

        //遍历全部时间
        while (!localDateMin.equals(localDateMax.plusDays(1))){
            Detail detail = null;
            DetailVO.Student studentDetail = new DetailVO.Student();
            //搜索该同学当天学习的最后章节
            Integer endChapter = detailMapper.selectByNameAndDate(name,localDateMin,localDateMin.plusDays(1));
            if(endChapter!=null){
                detail = detailMapper.selectDetail(endChapter);
                String chapter = detail.getName() +"_"+detail.getChapter()+"_"+detail.getContent();
                studentDetail.setTime(localDateMin);
                studentDetail.setChapter(chapter);
                list.add(studentDetail);
            }
            localDateMin = localDateMin.plusDays(1);
        }

        detailVO.setDetail(list);
        return Result.success(detailVO);
    }






}
